﻿package com.wandian.controller
{  
    import com.wandian.data.GameData;
    import com.wandian.model.vo.RoomVo;
    import com.wandian.view.mediator.GameRoomMediator;
    import com.wandian.view.mediator.MaskGameMediator;
    
    import flash.display.MovieClip;
    
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.command.*;

    /**
     * 游戏进出控制命令
	 * ENTER_GAME_ROOM 正式进房间
	 * OUT_GAME_ROOM_RSP
     * @author long 2012-10-22
     */
    public class GameRoomCommand extends SimpleCommand
    {
        private var _global:GameData;
        private var _mcGameMain:MovieClip;

        public function GameRoomCommand()
        {
            this._global = GameData.getInstance();
            return;
        }

        override public function execute(notification:INotification) : void
        {
            var data:Object = null;
            super.execute(notification);
            switch(notification.getName())
            {
                case CLZNoticeName.ENTER_GAME_ROOM:
                {
                    this.enterRoom(notification);
                    break;
                }
                case CLZNoticeName.OUT_GAME_ROOM_RSP:
                {
                    data = notification.getBody();
                    if (this._global.userInfo.userVo.userid == data.userid)
                    {
                        this.outRoom(notification);
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        /**
         *进入游戏房间
         * @param notification
         */
        private function enterRoom(notification:INotification) : void
        {
            GameData._objRoom = notification.getBody() as RoomVo;
            GameData.IS_UPDATE_SERVER = false;
            if (!this._mcGameMain)
            {
                this._mcGameMain = GameData.getMovieClip("GameMain");
            }
            if (!Texas.I._mcGameView.contains(this._mcGameMain))
            {
                Texas.I._mcGameView.addChild(this._mcGameMain);
            }
            facade.registerMediator(new GameRoomMediator(this._mcGameMain));
            return;
        }

        /**
         * 离开游戏房间
         * @param notification
         */
        private function outRoom(notification:INotification) : void
        {
            GameData._objRoom = null;
            Texas.I._mcGameView.removeAll();
            this._mcGameMain = null;
            facade.removeMediator(MaskGameMediator.NAME);
            facade.removeMediator(GameRoomMediator.NAME);
            return;
        }

    }
}
